package com.ryhx.ssm.utils;

import java.net.URLEncoder;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("all")
public class StringUtils {
	/**
	 * 如果str为null，返回“”,否则返回str
	 * 
	 * @param str
	 * @return
	 */
	public static String isNull(String str) {
		if (str == null) {
			return "";
		}
		// v1.6.7.1 去除参数前后空格 xx 2013-11-04 start
		return str.trim();
		// v1.6.7.1 去除参数前后空格 xx 2013-11-04 end
	}

	public static String isNull(Object o) {
		if (o == null) {
			return "";
		}
		String str="";
		if(o instanceof String){
			str=(String)o;
		}else{
			str=o.toString();
		}
		return str;
	}
	
	// v1.6.5.3 RDPROJECT-147 xx 2013.09.11 start
	/**
	 * 检验手机号
	 * @param phone
	 * @return
	 */
	public static boolean isPhone(String phone){
		phone = isNull(phone);
		Pattern regex = Pattern
				.compile("^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))\\d{8})$");
		Matcher matcher = regex.matcher(phone);
		boolean isMatched = matcher.matches();
		return isMatched;
	}
	// v1.6.5.3 RDPROJECT-147 xx 2013.09.11 end

	// v1.6.6.2 注册时加强真实姓名验证 start
	/**
	 * 检查是否全中文，返回true 表示是 反之为否
	 * @param realname
	 * @return
	 */
	public static boolean isChinese(String realname){
		realname = isNull(realname);
		// v1.6.7.1 RDPROJECT-423 xx 2013-11-15 start
		Pattern regex = Pattern.compile("[\\u4e00-\\u9fa5]{2,25}");
		// v1.6.7.1 RDPROJECT-423 xx 2013-11-15 end
		Matcher matcher = regex.matcher(realname);
		boolean isMatched = matcher.matches();
		return isMatched;
	}
	// v1.6.6.2 注册时加强真实姓名验证 end
	
	/**
	 * 检查email是否是邮箱格式，返回true表示是，反之为否
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		email = isNull(email);
		Pattern regex = Pattern
				.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		Matcher matcher = regex.matcher(email);
		boolean isMatched = matcher.matches();
		return isMatched;
	}
	
	/**
	 * 检查身份证的格式，返回true表示是，反之为否
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isCard(String cardId) {
		cardId = isNull(cardId);
		//身份证正则表达式(15位) 
		Pattern isIDCard1=Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$"); 
		//身份证正则表达式(18位) 
		Pattern isIDCard2=Pattern.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$"); 
		Matcher matcher1= isIDCard1.matcher(cardId);
		Matcher matcher2= isIDCard2.matcher(cardId);
		boolean isMatched = matcher1.matches()||matcher2.matches();
		return isMatched;
	}

	/**
	 * 判断字符串是否为整数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isInteger(String str) {
		if (isEmpty(str)) {
			return false;
		}
		Pattern regex = Pattern.compile("\\d*");
		Matcher matcher = regex.matcher(str);
		boolean isMatched = matcher.matches();
		return isMatched;
	}

	/**
	 * 判断字符串是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		if (isEmpty(str)) {
			return false;
		}

		Pattern regex = Pattern.compile("(-)?\\d*(.\\d*)?");
		Matcher matcher = regex.matcher(str);
		boolean isMatched = matcher.matches();
		return isMatched;
	}
	
	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断字符串是否不为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	/**
	 * 首字母大写
	 * 
	 * @param s
	 * @return
	 */
	public static String firstCharUpperCase(String s) {
		StringBuffer sb = new StringBuffer(s.substring(0, 1).toUpperCase());
		sb.append(s.substring(1, s.length()));
		return sb.toString();
	}
	
	public static String hideChar(String str,int len){
		if(str==null) return null;
		char[] chars=str.toCharArray();
		for(int i=1;i>chars.length-1;i++){
			if(i<len){
				chars[i]='*';
			}
		}
		str=new String(chars);
		return str;
	}
	
	public static String hideFirstChar(String str,int len){
		if(str==null) return null;
		char[] chars=str.toCharArray();
		if(str.length()<=len){
			for(int i=0;i<1;i++){
				chars[i]='*';
			}
		}else{
			for(int i=0;i<len;i++){
				chars[i]='*';
			}
		}
		str=new String(chars);
		return str;
	}
	
	public static String hideLastChar(String str,int len){
		if(str==null) return null;
		char[] chars=str.toCharArray();
		if(str.length()<=len){
			for(int i=0;i<chars.length;i++){
				chars[i]='*';
			}
		}else{
			for(int i=chars.length-1;i>chars.length-len-1;i--){
				chars[i]='*';
			}
		}
		str=new String(chars);
		return str;
	}
	
	//处理手机号 170****1234
	@SuppressWarnings("all")
	public  static String subPhone(String phone){
		if(phone==null||phone.equals(""))return null;
		String phone1=phone.substring(0, 3);
		String phone2=phone.substring(7);
		String subPhone=phone1+"****"+phone2;
		return subPhone;
	}
	/**
	 * 处理展示真实姓名
	 * */
	@SuppressWarnings("all")
	public static String subRealName(String realname,String sex,String phone){
		String realName="";
		if(realname==null||realname.equals("")){
			if(phone!=null||phone.equals("")){
				realName="新客户";
			}else{
				realName=subPhone(phone);
			}
		}
		if(realname.length()==2||realname.length()==3){
				realName=realname.substring(0, 1)+"**";
		}else if(realname.length()>3){
				realName=realname.substring(0, 2)+"**";
		}else{
				realName=realname.substring(0, 1)+"**";
		}
		return realName;
	}
	/**
	 * 
	 * @return
	 */
	public static String format(String str,int len){
		if(str==null) return "-";
		if(str.length()<=len){
			int pushlen=len-str.length();
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<pushlen;i++){
				sb.append("0");
			}
			sb.append(str);
			str=sb.toString();
		}else{
			String newStr=str.substring(0, len);
			str=newStr;
		}
		return str;
	}
	
	public static String contact(Object[] args){
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<args.length;i++){
			sb.append(args[i]);
			if(i<args.length-1){
				sb.append(",");
			}
		}
		return sb.toString();
	}
	
	/**
	 * 是否包含在以“，”隔开字符串内
	 * @param s
	 * @param type
	 * @return
	 */
	public static boolean isInSplit(String s,String type){
		if(isNull(s).equals("")){
			return false;
		}
		List<String> list=Arrays.asList(s.split(","));
		if(list.contains(type)){
			return true;
		}
		return false;
	}
	
	public static boolean isBlank(String str){
		return StringUtils.isNull(str).equals("");
	}
	
	public synchronized static String generateTradeNO(long userid,String type){
		String s;
		s = type + userid + getFullTimeStr();
		return s;
	}
	
	public static String getFullTimeStr(){
		String s=DateUtils.dateStr3(Calendar.getInstance().getTime());
		return s;
	}

	public static String array2Str(Object[] arr){
		StringBuffer s=new StringBuffer();
		for(int i=0;i<arr.length;i++){
			s.append(arr[i]);
			if(i<arr.length-1){
				s.append(",");
			}
		}
		return s.toString();
	}
	
	public static String array2Str(int[] arr){
		StringBuffer s=new StringBuffer();
		for(int i=0;i<arr.length;i++){
			s.append(arr[i]);
			if(i<arr.length-1){
				s.append(",");
			}
		}
		return s.toString();
	}
	
	
	/**
	 * 指定起始位置字符串隐藏
	 * 
	 * @param str
	 * @param index1
	 * @param index2
	 * @return
	 */
	public static String hideStr(String str, int index1, int index2) {
		if (str == null)
			return null;
		String str1 = str.substring(index1, index2);
		String str2 = str.substring(index2);
		String str3 = "";
		if (index1 > 0) {
			str1 = str.substring(0, index1);
			str2 = str.substring(index1, index2);
			str3 = str.substring(index2);
		}
		char[] chars = str2.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			chars[i] = '*';
		}
		str2 = new String(chars);
		String str4 = str1 + str2 + str3;
		return str4;
	}

	// 四舍五入保留两位小数点
	public static String SetNumberFractionDigits(String str) {
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(2);
		nf.setMinimumFractionDigits(2);
		return nf.format(Float.valueOf(str));
	}
	public static String getMonday(String the_rq){
	     int n=getXC_days(the_rq);
	     //System.out.println("n="+n);
	     n=n*-1;
	     return Q_N_Day(n,the_rq);
	 }

	 

	//获取输入日期的星期天日期

	 public static String getSunday(String the_rq){
	      int n=getXC_days(the_rq);
	      //System.out.println("n="+n);
	      n=(6-(n*-1))*-1;
	      return Q_N_Day(n,the_rq);
	  }

	// 获得输入日期与本周一相差的天数
	 public static int getXC_days(String rq){
	     SimpleDateFormat formatYMD=new SimpleDateFormat("yyyy-MM-dd");//formatYMD表示的是yyyy-MM-dd格式
	     SimpleDateFormat formatD=new SimpleDateFormat("E");//"E"表示"day in week"
	     Date d=null;
	     String weekDay="";
	     int xcrq=0;
	     try{
	         d=formatYMD.parse(rq);//将String 转换为符合格式的日期
	         weekDay=formatD.format(d);
	         if(weekDay.equals("星期一")){
	             xcrq=0;
	         }else{
	             if(weekDay.equals("星期二")){
	                 xcrq=-1;
	             }else{
	                 if(weekDay.equals("星期三")){
	                     xcrq=-2;
	                 }else{
	                     if(weekDay.equals("星期四")){
	                         xcrq=-3;
	                     }else{
	                         if(weekDay.equals("星期五")){
	                             xcrq=-4;
	                         }else{
	                             if(weekDay.equals("星期六")){
	                                 xcrq=-5;
	                             }else{
	                                 if(weekDay.equals("星期日")){
	                                     xcrq=-6;
	                                 }

	                             }

	                         }

	                     }
	                 }
	             }
	         }
	     }catch (Exception e){
	         e.printStackTrace();
	     }
	     return xcrq;
	 }

	 public static String Q_N_Day(int N,String d1){//
	        String []d2=d1.split("-");
	        int year=Integer.parseInt(d2[0]);
	        int month=Integer.parseInt(d2[1]);
	        int day=Integer.parseInt(d2[2]);
	        if(day-N<=0){
	            if(month==1){
	               year=year-1;
	               month=12;
	               day = day + 31-N;
	            }else{
	                month=month-1;
	                if (month == 2) {
	                    if (year % 4 == 0) {
	                        day = day + 29-N;
	                    } else {
	                        day = day + 28-N;
	                    }
	                }else{
	                    if(month==4||month==6||month==9||month==11){
	                        day=day+30-N;
	                    }else{
	                        day=day+31-N;
	                    }
	                }
	            }
	        }else{
	            ///////////////////////////////////////////////////////////////////////////////////
	            if(month==12){
	                if((day-N)>31){
	                    year=year+1;
	                    month=1;
	                    day=(day-N)-31;
	                }else{
	                    day=day-N;
	                }
	            }else{
	                if (month == 2) {
	                    if (year % 4 == 0) {
	                        if((day-N)>29){
	                            month++;
	                            day=(day-N)-29;
	                        }else{
	                            day=day-N;
	                        }
	                    } else {
	                        if((day-N)>28){
	                            month++;
	                            day=(day-N)-28;
	                        }else{
	                            day=day-N;
	                        }
	                    }
	                }else{
	                    if(month==4||month==6||month==9||month==11){
	                        if((day-N)>30){
	                            month++;
	                            day=(day-N)-30;
	                        }else{
	                            day=day-N;
	                        }
	                    }else{
	                        if((day-N)>31){
	                            month++;
	                            day=(day-N)-31;
	                        }else{
	                            day=day-N;
	                        }
	                    }
	                }
	            }

	 

	            //day=day-N;
	        }
	        String str=String.valueOf(year);
	        if(month<10){
	            str=str+"-0"+String.valueOf(month);
	        }else{
	            str=str+"-"+String.valueOf(month);
	        }
	        if(day<10){
	            str=str+"-0"+String.valueOf(day);
	        }else{
	            str=str+"-"+String.valueOf(day);
	        }

	        return str;
	    }

	
	
	public static String fillTemplet(String templet, String phstr, String[] paras){
		StringBuffer templetSB = new StringBuffer(templet);
		int i = 0;
		while(templetSB.indexOf(phstr) >= 0 && i < paras.length){
			templetSB.replace(templetSB.indexOf(phstr), templetSB.indexOf(phstr)+phstr.length(), paras[i]);
			i++;
		}
		return templetSB.toString();
	}
	
	//V1.6.6.1 RDPROJECT-226 liukun 2013-09-26 end
	
	//V1.6.5.3 RDPROJECT-142 liukun 2013-09-11 start
	public static int[] strarr2intarr(String[] strarr){
		int[] result = new int[strarr.length];
		for(int i=0;i<strarr.length;i++)
		{
		   result[i] = Integer.parseInt(strarr[i]);
		}
		return result;
	}
	
	/**
	 * 大写字母转成“_”+小写
	 * @param str
	 * @return
	 */
	public static String toUnderline(String str){
		char[] charArr=str.toCharArray();
		StringBuffer sb=new StringBuffer();
		sb.append(charArr[0]);
		for(int i=1;i<charArr.length;i++){
			if(charArr[i]>='A'&&charArr[i]<='Z'){
				sb.append('_').append(charArr[i]);
			}else{
				sb.append(charArr[i]);
			}
		}
		return sb.toString().toLowerCase();
	}
	
	/**
     * 保留有效位数, 超过长度从左保留, 不足长度原样输出 abc 2 ->ab
     * 
     * @param inStr
     *            输入字符串
     * @param length
     *            长度
     * @return
     */
    public static String subString(String inStr, int length)
    {
        if (inStr == null || inStr.length() == 0 || length <= 0)
        {
            return "";
        }
        else if (inStr.length() >= length)
        {
            return inStr.substring(0, length);
        }
        else
        {
            return inStr;
        }
    }

    /**
     * 保留有效位数, 超过长度从右保留, 不足长度原样输出 2 abc -> bc
     * 
     * @param inStr
     *            输入字符串
     * @param length
     *            长度
     * @return
     */
    public static String subString(int length, String inStr)
    {
        if (inStr == null || inStr.length() == 0 || length <= 0)
        {
            return "";
        }
        else if (inStr.length() >= length)
        {
            return inStr.substring(inStr.length() - length, inStr.length());
        }
        else
        {
            return inStr;
        }
    }

    /**
     * 左补零 按有效位数截取保留右侧 null->"" abc 5 -> 00abc
     * 
     * @param pinseed
     * @param i
     * @return
     */
    public static String lengthPinseed(String pinseed, int i)
    {
        if (i <= 0)
        {
            return "";
        }
        if (pinseed == null)
        {
            pinseed = "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        try
        {
            for (int j = 0; j < i; j++)
            {
                stringBuffer.append("0");
            }

            if (pinseed.length() < i)
            {
                pinseed = stringBuffer.toString() + pinseed;
            }
            if (pinseed.length() > i)
            {
                pinseed = pinseed.substring(pinseed.length() - i, pinseed.length());
            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            stringBuffer.replace(0, stringBuffer.length(), "");
        }
        return pinseed;
    }
    
    /**
     * 左补a 默认为"0" 按有效位数截取保留右侧 null->"" abc 5 -> 00abc
     * 
     * @param pinseed 源字符串
     * @param i 有效位
     * @param a 填充元素
     * @return
     */
    public static String lengthPinseed2(String pinseed, int i, String a)
    {
        if (i <= 0)
        {
            return "";
        }
        if (pinseed == null)
        {
            pinseed = "";
        }
        if (a == null ){
            a="0";
        }
        StringBuffer stringBuffer = new StringBuffer();
        try
        {
            for (int j = 0; j < i; j++)
            {
                stringBuffer.append(a);
            }

            if (pinseed.length() < i)
            {
                pinseed = stringBuffer.toString() + pinseed;
            }
            if (pinseed.length() > i)
            {
                pinseed = pinseed.substring(pinseed.length() - i, pinseed.length());
            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            stringBuffer.replace(0, stringBuffer.length(), "");
        }
        return pinseed;
    }
    
    public static String chStrE (String str){
    	try {
    		
			return	URLEncoder.encode(str, "UTF-8");//new String (str.getBytes("UTF-8"),"gb2312");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
    }
    
    /**
	 * 隐藏文字
	 * @param a
	 * @return
	 */
    public  static String getStrHide(String a){
		String tmp = "***";
		if(a!=null && a.length()>3 ){
			StringBuffer yCod = new StringBuffer();
				for (int i = 0; i < a.length()-2; i++) {
					yCod .append("*");
				}
			tmp =a.charAt(0)+yCod.toString()+a.charAt(a.length()-1);
		}
		return tmp;
	}
    
    
    /**
	 * 普通
	 */
	private static String[] denied_all = { "drop", "select", "declare",
			"insert", "update", "delete", "cast", "alert", "create", "where",
			"join", "iframe", "alter", "\"", "like", "%22", "'", "%27",
			"script", "confirm", "modify", "</", "%3b", "rename", "%3c", "%3e",
			"unicode", ";", "<", "grant", ">", };
    /**
	 * 关键字过滤判断
	 *<pre> </pre>
	 * @param text
	 * @return
	 */
	public static boolean isDeniedAll(String text){
		return isDenied(text, denied_all);
	}
	
	/**
	 * 是否存在需要过滤的字符
	 * @param text
	 * @return 存在返回true
	 */
	public static boolean isDenied(String text,String[] denied){
		text= StringUtils.isNull(text).toLowerCase();//增加过滤器判断条件, 参数转化成小写, 再判断,
		for(int j=0;j<denied.length;j++){
			String denied_Text=StringUtils.isNull(denied[j]);
			if(text.indexOf(denied_Text)>-1){
				System.out.println(denied_Text);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 数组追加元素
	 * @param old 原数组
	 * @param para	参数
	 * @return
	 */
	public static String[] addStrArray(String[] old, String... para){
		  String[] res = new String[old.length + para.length];
		  for (int j = 0; j < old.length; ++j) {
			  res[j] = old[j];
		  }
		  for (int j = 0; j < para.length; ++j) {
			  res[old.length + j] = para[j];
		  }
		  return res;
	}
	
	/**
	 * 合并 数组
	 * @param byte_1
	 * @param byte_2
	 * @return
	 */
	public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
		byte[] byte_3 = new byte[byte_1.length + byte_2.length];
		System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
		System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
		return byte_3;
	}
	
	/**
	 * int to byte
	 */
	public static byte[] int2byte(int res) {
		byte[] targets = new byte[4];
		targets[0] = (byte) (res & 0xff);// 最低位
		targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
		targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
		targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
		return targets;
	} 
	
	/**
	 * byte to int
	 */
	public static int byte2int(byte[] res) {
		// 一个byte数据左移24位变成0x??000000，再右移8位变成0x00??0000
		int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) | ((res[2] << 24) >>> 8) | (res[3] << 24);
		return targets;
	}
	
	private static String[] denied_keywords={"paypassword","password","confirm_password","oldpassword","newpassword","newpassword1","oldpasswordpay","newpasswordpay","newpassword1pay"};
	
	/**
	 * 是否存在需要过滤的字符
	 * @param text
	 * @return 存在返回true
	 */
	public static boolean isDeniedKeywords(String text){
		for(int j=0;j<denied_keywords.length;j++){
			String denied_Text=StringUtils.isNull(denied_keywords[j]);
			if(StringUtils.isNull(text).toLowerCase().indexOf(denied_Text)>-1){
				return true;
			}
		}
		return false;
	}
	
    
    /**
     * 贝付
     */
	private static String[] denied_texts={"'",";","insert","declare","drop","create ",
		"alter","rename","join","where","like","cast","script","iframe","%27",
		"%3b","%22","</","<",">","grant","%3c","%3e","unicode","alert","confirm","\""};
	private static String[] denied_htmls={"select","insert","declare","drop","delete","create ",
		"modify","alter","rename","join","where","like","cast","script","iframe","update","grant","%3c","%3e","unicode"};
	
	public static boolean isDeniedText(String text){
		return isDenied(text,denied_texts);
	}
	
	private static final String key = "\\$\\{\\w+\\}";
	
	/**
	 * 测试版
	 * @param format ${key1}小明学校${key2}球${key1}
	 * @param map {key1=在,key2=踢}
	 * @return 在小明学校踢球在
	 */
	@Deprecated
	public static String format(String format,Map<String, String> map){
		Matcher m = Pattern.compile(key).matcher(format);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			String param = m.group(); // ${xx}
			Object value = map.get(param.substring(2, param.length() - 1));
			m.appendReplacement(sb, value == null ? "" : value.toString());
		}
		m.appendTail(sb);
		return sb.toString();
	}
	
	private static final String key2 = "(.*?)";
	
	private static final String key3 = "($)";
	
	/**
	 * 测试版
	 * @param format 错误码${key1}说明${key2}
	 * @param original "错误码Code说明 R NOT NULL 响应码 "
	 * @return {key1=Code, key2= R NOT NULL 响应码 }
	 */
	@Deprecated
	public static Map<String, String> parse(String format,String original){
		Map<Integer,String> tMap= new HashMap<Integer,String>();
		Matcher m = Pattern.compile(key).matcher(format);
		StringBuffer sb = new StringBuffer();
		int ii= 1;
		while (m.find()) {
			String param = m.group(); // ${xx}
			tMap.put(ii, param.substring(2, param.length() - 1));
			m.appendReplacement(sb, key2);
			ii++;
		}
		m.appendTail(sb);
		sb.append(key3);
		Pattern pattern = Pattern.compile(sb.toString());// 
		Matcher matcher = pattern.matcher(original);
		Map<String,String> rMap= new HashMap<String,String>();
		if(matcher.find()){
			int group = matcher.groupCount();
			for (int i = 1; i < group; i++) {
				String param = matcher.group(i); // ${xx}
				rMap.put(tMap.get(i), param);
			}
		}
		return rMap;
	}
	
	/**
	 * 删除匹配字符
	 * 
	 * @param regEx 正则表达式 [^a-zA-Z0-9=]
	 * @param text 文本
	 * @return
	 * @throws Exception
	 */
	public static String charFilter(String regEx, String text) throws Exception {
		// 只允许字母和数字
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(text);
		return m.replaceAll("");
	}
	
	public static String paraToFirst(String[] para) {
		if(para==null){
			return "";
		}else if (para instanceof String[]){
			if(para.length>0){
				return para[0];
			}else{
				return "";
			}
		}else{
			return "";
		}
	}

	/**
	 * 用户名 
	 * 首位字母+任意(数字,字母,下划线)
	 * @param username
	 * @return
	 */
	public static boolean isUsername(String username) {
		if (username == null || username.isEmpty()) {
			return false;
		}
		Pattern pattern = Pattern.compile("([a-zA-Z])([a-zA-Z_0-9]*)");
		Matcher matcher = pattern.matcher(username);
		return matcher.matches();
	}
}
